package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 李旭
 * @date 2021/10/27 15:46
 * @Description:
 */
@Service
public class AlipayServiceImpl implements AlipayService {


    //SDK远程调用 支付宝公司
    //支付宝的客户端
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private PaymentService paymentService;


    //统一收单下单并支付接口
    @Override
    public String submit(Long orderId) {

        //提取下面的生成支付表
        //1:生成财务系统所需要的 支付表（钱相关信息）  支付宝 微信 中国银行 哪家银行 支付类型
        //注意问题  幂等性问题
        PaymentInfo paymentInfo = paymentService.insert(orderId, PaymentType.ALIPAY);
        //2: 接口名称  统一收单下单并支付接口 alipay.trade.page.pay
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //异步回调
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        //同步回调
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //请求参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", paymentInfo.getOutTradeNo());//支付宝 交易编号 唯一 必选
        bizContent.put("total_amount", paymentInfo.getTotalAmount());//总金额  一个亿必选
        bizContent.put("subject", paymentInfo.getSubject());//必选
        bizContent.put("qr_pay_mode", "2");//必选  同学是安卓手机的  同学如果是苹果手机 1
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//注：目前仅支持FAST_INSTANT_TRADE_PAY必选
        //Calendar calendar  = Calendar.getInstance();
        //calendar.add(Calendar.HOUR,1);
        //bizContent.put("time_expire", calendar.getTime());//二维码的  绝对时间  16:30  可选
        request.setBizContent(bizContent.toString());
        //3:发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = null;
        try {
            alipayTradePagePayResponse = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(alipayTradePagePayResponse.isSuccess()){
            System.out.println("调用成功");
            return alipayTradePagePayResponse.getBody();
        }else{
            System.out.println("调用失败");
        }
        return null;
    }

    //统一收单下单退款接口
    @Override
    public void refund(String outTradeNo) {

        //1:查询支付表
        PaymentInfo paymentInfo = paymentService.selectPayment(outTradeNo);

        //alipay.trade.refund
        AlipayTradeRefundRequest alipayTradeRefundRequest =
                new AlipayTradeRefundRequest();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no",paymentInfo.getOutTradeNo());
        jsonObject.put("refund_amount",paymentInfo.getTotalAmount());

        alipayTradeRefundRequest.setBizContent(jsonObject.toString());
        try {
            AlipayTradeRefundResponse alipayTradeRefundResponse =
                    alipayClient.pageExecute(alipayTradeRefundRequest);
            if(alipayTradeRefundResponse.isSuccess()){
                System.out.println("退款成功");
            }else{
                System.out.println("退款失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

    }



    //统一收单下单关闭交易接口


    //..........
}
